package com.ellricky.baselib.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.andr.common.util.ViewUtil
import com.andr.common.widget.refresh.RefreshFlag
import com.andr.common.widget.refresh.RefreshView
import com.andr.common.widget.refresh.RvRefreshListener
import com.ellricky.baselib.R
import com.ellricky.baselib.adapter.CommonGridItemDecoration
import com.ellricky.baselib.adapter.GridItemDecoration
import kotlinx.android.synthetic.main.common_refresh_rv_view.view.*

const val PAGE_SIZE = 15
const val PAGE_START = 1

class NormalRefreshRvView : RefreshView {

    private var flHeader: FrameLayout? = null
    private var mRv: RecyclerView? = null
    private var curPage = 0
    private var mListener: RvRefreshListener? = null
    private var mLayoutManager: RecyclerView.LayoutManager? = null

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)

    constructor(context: Context, attributeSet: AttributeSet?, def: Int) : super(
        context,
        attributeSet,
        def
    ) {
        curPage = 1
        this.setContentView(R.layout.common_refresh_rv_view)
        flHeader = findViewById<View>(R.id.flHeader) as FrameLayout
        mRv = findViewById<View>(R.id.rv) as RecyclerView
        mRv?.layoutManager = LinearLayoutManager(context)

    }

    fun setHeaderView(contentRes: Int) {
        flHeader?.removeAllViews()
        val view = ViewUtil.createView(mContext, contentRes)
        if (view != null) {
            flHeader?.addView(view)
        }
    }

    override fun loadContent(refresh: Boolean) {
        if (refresh) {
            curPage = PAGE_START
        }
        if (mListener != null) {
            mListener?.loadData(refresh)
        }
    }

    fun getCurPage(): Int {
        return curPage
    }

    fun getPageSize(): Int {
        return PAGE_SIZE
    }

    fun init(listener: RvRefreshListener?) {
        this.init(listener, 1)
    }

    fun init(listener: RvRefreshListener?, @RefreshFlag refreshFlag: Int) {
        super.init(refreshFlag)
        mListener = listener
        if (mListener != null) {
            mListener?.setAdapter(mRv)
        }
    }

    fun setListener(listener: RvRefreshListener?) {
        mListener = listener
        if (mListener != null) {
            mListener?.setAdapter(mRv)
        }
    }

    fun <T> onLoadFinish(entityList: List<T>, refresh: Boolean) {
        mSmartRefreshLayout.visibility = View.VISIBLE
        if (refresh) {
            mSmartRefreshLayout.finishRefresh()
            mSmartRefreshLayout.resetNoMoreData()
            if (entityList.isEmpty()) {
                mSmartRefreshLayout.finishRefreshWithNoMoreData()
            } else {
                ++curPage
            }
            if (mListener != null) {
                mListener?.updateAdapterData(true)
            }
        } else if (refreshFlag == 1) {
            if (entityList.isEmpty()) {
                mSmartRefreshLayout.finishLoadMoreWithNoMoreData()
            } else {
                mSmartRefreshLayout.finishLoadMore()
                ++curPage
                if (mListener != null) {
                    mListener?.updateAdapterData(false)
                }
            }
        }
    }

    fun onRefreshFailed(refresh: Boolean) {
        if (refresh) {
            mSmartRefreshLayout.finishRefresh(false)
        } else {
            mSmartRefreshLayout.finishLoadMore(false)
        }
    }

    fun setLayoutManager(manager: RecyclerView.LayoutManager?) {
        if (manager != null) {
            this.mLayoutManager = manager
            rv?.layoutManager = mLayoutManager
        }
    }

    fun addItemDecoration(decoration: RecyclerView.ItemDecoration) {
        mRv?.addItemDecoration(decoration)
    }
}